class WordList {
    constructor(words) {
        this.head = {
            data: {},
            strangeDegree: 3,
            prior: this.head,
            next: this.head
        };
        this.tail = this.head;
        this.current = this.head;
        this.length = 1;
        for (const word of words) {
            this.append(word);
        }
        this.next();
    }

    //添加节点
    append(data) {
        const node = {
            data,
            prior: null,
            next: null,
        };
        node.strangeDegree = this.strange;
        node.word = this.word;
        node.prior = this.tail;
        this.tail.next = node;
        node.next = this.head;
        this.tail = node;
        this.length += 1;
    }

    //删除节点
    delete() {
        this.current.prior.next = this.current.next;
        this.current.next.prior = this.current.prior;
        this.length -= 1;
    }

    // 改变当前节点指针
    next() {
        if (this.length === 1)
            throw "错误1:访问的链表为空链表";
        else if (this.current.next === this.head)
            this.current = this.current.next.next;
        else
            this.current = this.current.next;
    }

    // 获得当前节点的数据
    data() {
        if (this.current.data === {} || this.current.data === null)
            throw "错误:访问的节点为空节点"
        else
            return this.current.data;
    }

    // 返回下一个节点对象
    getNext(startNode) {
        var node = startNode;
        if (this.length === 1)
            throw "错误2:访问的链表为空链表";
        else if (node.next === this.head)
            return node.next.next;
        else
            return node.next;
    }

    // 生成[minNum,maxNum] 的随机数
    randomNum(minNum, maxNum) {
        switch (arguments.length) {
            case 1:
                return parseInt(Math.random() * minNum + 1, 10);
                break;
            case 2:
                return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
                break;
            default:
                return 0;
                break;
        }
    }

    // 生成互不相同的三个随机数，用于随机选取单词
    getThreeNums() {
        let array = new Array();
        var random = 1;
        if (this.length < 6) {
            for (let i = 1; i <= this.length - 2; i++) {
                array.push(i);
            }
            return array;
        }

        while (array.length < 3) {
            random = this.randomNum(1, this.length - 2);
            if (!array.includes(random))
                array.push(random);
        }
        return array;
    }

    // 随机生成四个单词节点，用于取中文释义
    getFourNodes() {
        try {
            // 检查链表是否为空或者只有头节点
            if (this.length <= 1) {
                throw new Error("链表为空或只有一个节点，无法获取四个节点");
            }

            let result = new Array();
            let array;

            // 获取随机偏移量数组
            try {
                array = this.getThreeNums();
            } catch (e) {
                console.warn("getThreeNums 方法出错:", e);
                // 如果单词数量不足，使用替代方法
                array = [];
                let maxAttempts = Math.min(this.length - 1, 3);
                for (let i = 0; i < maxAttempts; i++) {
                    array.push(i + 1); // 使用简单的递增偏移量
                }
            }

            // 如果 array 中的数字不足 3 个，重复使用已有的数字补足
            while (array.length < 3 && array.length > 0) {
                array.push(array[array.length % array.length]); // 循环重复已有数字
            }

            // 处理极端情况：如果仍然没有偏移量，添加默认值
            if (array.length === 0) {
                array = [1, 1, 1]; // 添加默认偏移量
            }

            // 遍历 array，根据偏移量获取节点
            for (var i = 0; i < array.length; i++) {
                var offset = array[i];
                var tempNode = this.current;
                let attempts = 0;

                // 使用偏移量获取节点，防止无限循环
                while (offset > 0 && attempts < this.length) {
                    tempNode = this.getNext(tempNode);
                    offset -= 1;
                    attempts += 1;
                }
                result.push(tempNode);
            }

            // 确保结果中包含当前节点
            let currentNodeIncluded = false;
            for (let i = 0; i < result.length; i++) {
                if (result[i] === this.current) {
                    currentNodeIncluded = true;
                    break;
                }
            }

            // 如果当前节点不在结果中，随机插入
            if (!currentNodeIncluded) {
                var index = this.randomNum(0, result.length);
                result.splice(index, 0, this.current);
            }

            // 确保返回4个结果
            while (result.length < 4 && this.length > 1) {
                // 添加一个随机节点
                let randomNode = this.current;
                let randomSteps = this.randomNum(1, Math.min(this.length - 1, 5));
                for (let i = 0; i < randomSteps; i++) {
                    randomNode = this.getNext(randomNode);
                }
                // 检查是否重复
                let isDuplicate = false;
                for (let i = 0; i < result.length; i++) {
                    if (result[i].data.wordID === randomNode.data.wordID) {
                        isDuplicate = true;
                        break;
                    }
                }
                if (!isDuplicate) {
                    result.push(randomNode);
                }
            }

            // 如果还是无法获取足够的节点，复制已有节点
            while (result.length < 4) {
                result.push(result[result.length % result.length]);
            }

            return result;
        } catch (error) {
            console.error("getFourNodes 方法出错:", error);
            // 返回一个带有当前节点的数组，其余位置用相同节点填充
            let fallbackResult = [];
            for (let i = 0; i < 4; i++) {
                fallbackResult.push(this.current);
            }
            return fallbackResult;
        }
    }
}

export default WordList;